<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Upload;

class DownController extends Controller {

    public function downadd(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        if (!IS_POST) {
            $this->display();
        }else{
            $down=M('down');

            //接收文件 调用文件上传
            if ($_FILES['pic']['error'] == 0) {
                $upload = new \Admin\Controller\MyUploadController();
                $info = $upload->upload();
                if (!$info) {
                    $this->error("上传失败");
                } else {
                    $_POST['downpic'] = $info['pic'];
                }
            }

            $_POST['is_del']=0;
            $_POST['ctime']=time();
            $_POST['utime']=time();
            if ($down->add($_POST)) {
                $this->success("添加成功",(U('Admin/down/downlist')));
            }else{
                $this->error("添加失败");
            }
        }
    }
    public function downlist(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        $down = M('down');

        $count = $down->where(array('is_del' => '0'))->count();
        $Page  = new \Think\Page($count,10);
        $down = $down->where('is_del=0')->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();

        $this->assign('page',$show);
        $this->assign('rows',$down); 
        $this->display();
    }

    public function downedit(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        $downModel = D('down');
    	if (!IS_POST) {
            $down_id = I('id');
            $down  = $downModel->find($down_id); 
            $this->assign('down',$down); 
            $this->display();
    	}else{
            //接收文件 调用文件上传
            if ($_FILES['pic']['error'] == 0) {
                $upload = new \Admin\Controller\MyUploadController();
                $info = $upload->upload();
                if (!$info) {
                    $this->error("上传失败");
                } else {
                    $downModel->downpic = $info['pic'];
                }
            }
            $downModel->title=$_POST['title'];
            $downModel->url=$_POST['url'];
            $downModel->utime=time();
            if ($downModel->where('id='.$_POST['id'])->save()) {
                $this->success("修改成功",(U('Admin/down/downlist')));
            }else{
                $this->error("修改失败");
            }
        }
    }
    public function downdel(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        $down = M('down');
        $down->is_del = 1;
        if ($down->where('id='.$_GET['id'])->save()) {
            $this->success("删除成功",(U('Admin/down/downlist')));
        } else {
            $this->error("删除失败");
        }
    }
}